/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ramstorage;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Minute;
import thesis.ramstorage.application.NetworkControl;
import thesis.ramstorage.math.StatisticParameters;
import thesis.ramstorage.util.ChartRender;

/**
 *
 * @author minhhl
 */
class ChartMonitor implements Runnable {

    private NetworkControl netCtrl;
    private javax.swing.JPanel chartPanel;
    private SortedMap<Minute, Double> stdMap = new TreeMap<Minute, Double>();
    private List<Double> stdList = new ArrayList<Double>();
    public ChartMonitor(NetworkControl netCtrl, javax.swing.JPanel chartPanel) {
        this.netCtrl =netCtrl;
        this.chartPanel = chartPanel;
    }
    public void run() {
        while(true){
            StatisticParameters sp = this.netCtrl.statistic();
            ChartRender cr = new ChartRender("Time Line Monitor");///, ev.getNodeParams());
            try {
                Thread.sleep(5000);
                this.stdMap.put(new Minute(), sp.getStandardDeviation());
                if(this.stdMap.size() > 10){
                    this.stdMap.remove(this.stdMap.firstKey());
                }

//                this.stdList.add(sp.getStandardDeviation());
//                if(this.stdList.size() > 10){
//                    this.stdList.remove(0);
//                }
                 
                JFreeChart chart = cr.createTimeSeriesChart(stdMap);
//                JFreeChart chart = cr.lineChartRender(stdList);
                ChartPanel cp = new ChartPanel(chart);
                chartPanel.setPreferredSize(new java.awt.Dimension(500, 150));
                this.chartPanel.setLayout(new java.awt.BorderLayout());
                chartPanel.removeAll();
                chartPanel.add(cp, BorderLayout.CENTER);
                chartPanel.validate();
                /*
                 *
                 * 
                 * */

            } catch (InterruptedException ex) {
                Logger.getLogger(ChartMonitor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    
}
